package com.xixi.canal.client.util;

import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

public final class StringUtil {

    public static String trimToEmpty(String str) {
        return str == null ? "" : str.trim();
    }

    public static String getJarPathDir(String path) {
        String jarPath = path.replaceAll("^.*file:/", ""); // removes
        // file:/ and
        // everything
        // before it
        jarPath = jarPath.replaceAll("jar!.*", "jar"); // removes everything
        // after .jar, if .jar
        // exists in dirtyPath
        jarPath = jarPath.replaceAll("%20", " "); // necessary if path has
        // spaces within
        if (!jarPath.endsWith(".jar")) { // this is needed if you plan to run
            // the app using Spring Tools Suit play
            // button.
            jarPath = jarPath.replaceAll("/classes/.*", "/classes/");
        }
        return jarPath;
    }

    public static boolean hasText(String classLoaderPolicy) {
        return null != classLoaderPolicy && classLoaderPolicy.length() > 0;
    }

    public static String getDataSourceKey(String ...values) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            if (i != 0) {
                builder.append(".");
            }
            builder.append(values[i]);
        }

        return builder.toString();
    }

    public static String cleanColumn(String column) {
        if (column == null) {
            return null;
        }
        if (column.contains("`")) {
            column = column.replaceAll("`", "");
        }

        if (column.contains("'")) {
            column = column.replaceAll("'", "");
        }

        if (column.contains("\"")) {
            column = column.replaceAll("\"", "");
        }

        return column;
    }

    public static Map<String, String> stringToMap(String str) {

        Map<String, String> ret = new LinkedHashMap<>();

        if (!hasText(str)) return ret;

        String[] rows = str.split(",");
        for (String row : rows) {
            String[] cols = row.split("=");
            ret.put(cols[0], cols[1]);
        }

        return ret;
    }

    public static File getConfDirPath(String conf) {
        return getConfDirPath(conf, "");
    }

    public static File getConfDirPath(String conf, String subConf) {
        URL url = StringUtil.class.getClassLoader().getResource("");
        String path;
        if (url != null) {
            path = url.getPath();
        } else {
            path = new File("").getAbsolutePath();
        }
        File file = null;
        if (path != null) {
            file = new File(
                    path + File.separator + conf + File.separator + StringUtils.trimToEmpty(subConf));
            if (!file.exists()) {
                file = new File(path + StringUtils.trimToEmpty(subConf));
            }
        }
        if (file == null || !file.exists()) {
            throw new RuntimeException("Config dir not found.");
        }

        return file;
    }
}
